﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TVL;
using System.Xml;
using General;


namespace TVLL5A
{
    public class DineroL5A : Dinero
    {
        private int _koku;
        private int _bu;
        private int _zeni;
        private const string _rutaxsd = "../../../TVLL5A/DineroL5A.xsd";
        private string _fallo;

        public string Fallo
        {
            get { return this._fallo; }
        }

        public DineroL5A()
        {
            _koku = _bu = _zeni = 0;
            _fallo = "No";
        }

        public DineroL5A(int k, int b, int z)
        {
            _koku = k;
            _bu = b;
            _zeni = z;
            _fallo = "No";
        }

        public DineroL5A(string ruta)
        {
            _fallo = "No";
            this.cargar(ruta);
        }

        public DineroL5A(XmlElement nodo)
        {
            _fallo = "No";
            this.cargar(nodo);
        }

        public int koku
        {
            get { return this._koku; }
            set { this._koku = value; }
        }

        public int bu
        {
            get { return this._bu; }
            set { this._bu = value; }
        }

        public int zeni
        {
            get { return this._zeni; }
            set { this._zeni = value; }
        }

        public override void toXTVL(XmlTextWriter mixml)
        {
            mixml.WriteStartElement("dinero");
            mixml.WriteElementString("koku", this.koku.ToString());
            mixml.WriteElementString("bu", this.bu.ToString());
            mixml.WriteElementString("zeni", this.zeni.ToString());
            mixml.WriteEndElement();
        }

        public override bool guardar(string ruta)
        {
            XmlTextWriter mixml = new XmlTextWriter(ruta, System.Text.Encoding.UTF8);
            mixml.Formatting = System.Xml.Formatting.Indented;
            mixml.WriteStartDocument();
            this.toXTVL(mixml);
            mixml.WriteEndDocument();
            mixml.Close();
            XSDValidator validador = new XSDValidator(_rutaxsd, ruta);
            if (validador.validar())
            {
                this._fallo = validador.Fallo;
                return true;
            }
            else
            {
                if (System.IO.File.Exists(ruta))
                {
                    System.IO.File.Delete(ruta);
                }
                return false;
            }
        }

        public override bool cargar(string ruta)
        {
            XSDValidator verificar = new XSDValidator(_rutaxsd, ruta);
            if (!verificar.validar())
            {
                this._fallo = verificar.Fallo;
                return false;
            }
            else
            {
                XmlDocument xtvl = new XmlDocument();
                xtvl.Load(ruta);
                XmlNodeList kokus = xtvl.GetElementsByTagName("koku");
                XmlNodeList bus = xtvl.GetElementsByTagName("bu");
                XmlNodeList zenies = xtvl.GetElementsByTagName("zeni");
                this.koku = Convert.ToInt32(kokus[0].InnerText);
                this.bu = Convert.ToInt32(bus[0].InnerText);
                this.zeni = Convert.ToInt32(zenies[0].InnerText);
                return true;
            }
        }

        public override bool cargar(XmlElement nodo)
        {
            try
            {
                XmlNodeList kokus = nodo.GetElementsByTagName("koku");
                XmlNodeList bus = nodo.GetElementsByTagName("bu");
                XmlNodeList zenies = nodo.GetElementsByTagName("zeni");
                this.koku = Convert.ToInt32(kokus[0].InnerText);
                this.bu = Convert.ToInt32(bus[0].InnerText);
                this.zeni = Convert.ToInt32(zenies[0].InnerText);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}